#include"Search.hpp"
#include"cpp-httplib/httplib.h"
#include<memory>
#include"log.hpp"

static const std::string input = "/root/doc/output/RawDoc.txt";
int main()
{
     httplib::Server server;
     std::unique_ptr<Search_ns::Searcher> _searcher =std::make_unique<Search_ns::Searcher>();
     _searcher->InitIndex(input);
     ENABLESCREEN();

     server.Get("/s",[&_searcher](const httplib::Request& req,httplib::Response& resp){
     std::string param = req.get_param_value("word");
     if(param.empty())
     {
          LOG(ERROR,"不存在为word的参数"); 
          return;
     }
     
     //走到这里一定拿到了key值为word的参数
     std::string json_out;
     LOG(INFO,"用户搜索: %s",param.c_str());
     _searcher->Search(param,&json_out);
     if(!json_out.empty())
     {
     //   std::cout << json_out << std::endl;
       resp.set_content(json_out,"application/json; charset=utf-8");
     }
     });

     server.set_base_dir("./wwwroot");
     LOG(INFO, "服务器启动成功...");
     server.listen("0.0.0.0",8080);

}
